//
//  String + Extension.swift
//  正则表达式
//
//  Created by 亚杏 on 2017/6/2.
//  Copyright © 2017年 oms. All rights reserved.
//

import Foundation


extension String {

    //从当前字符串中，提取链接和文本
    //Swift 提供了元祖，一次性返回多个值
    //OC 可以返回字典、自定义对象，指针的指针
    func xyx_href() -> (link: String, text: String)? {

        //0> 匹配字符串
        let pattern = "<a href=\"(.*?)\".*?>(.*?)</a>"

        //1> 创建正则表达式, 如果pattern 失败， 抛出异常
        guard let regx = try? NSRegularExpression(pattern: pattern, options: []),
              let result = regx.firstMatch(in: self, options: [], range: NSRange(location: 0, length: characters.count))
        else{
            return nil
        }


        //3.获取结果
        let link = (self as NSString).substring(with: result.rangeAt(1))
        let text = (self as NSString).substring(with: result.rangeAt(2))

        print("\(link)----\(text)")
        return (link, text)

    }
   
}
